import {app} from './app'


const debug = require('debug')('api-manager-express:server');
const http = require('http');

/**
 * 设置端口号
 */
const port = 4002
app.set('port', port)

/**
 * 创建 Http Server
 */
const server = http.createServer(app)

server.listen(port)
server.on('error', (error: any) => {
    if (error.syscall != 'listen') {
        throw error;
    }

    const bind = 'Port ' + port;

    // handle specific listen errors with friendly messages
    switch (error.code) {
        case 'EACCES':
            console.error(bind + ' requires elevated privileges');
            process.exit(1);
            break;
        case 'EADDRINUSE':
            console.error(bind + ' is already in use');
            process.exit(1);
            break;
        default:
            throw error;
    }
})

server.on('listening', () => {
    const addr = server.address();
    const bind = typeof addr === 'string'
        ? 'pipe ' + addr
        : 'port ' + addr.port;
    debug('Listening on ' + bind);
})
